package fileProcessors.implementations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.DecimalFormat;

import fileProcessors.FileFormatException;
import fileProcessors.FileProcessor;

public class RoentgenAuswertung extends FileProcessor {
	private BufferedWriter outputWriter;
	private BufferedReader br;

	final static double a = 5.463E-10;
	final static double sqrt2 = Math.sqrt(2);

	boolean RUN_ABSORPTIONS_KOEFFIZIENT = true;
	private static double LAMBDA_TRHESHHOLD=1E-15;
	private final double d=2.5E-3;
	private final double rho=11.33;

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) throws FileFormatException {
		boolean RUN_ABSORPTIONS_KOEFFIZIENT = true;
		if (RUN_ABSORPTIONS_KOEFFIZIENT) {
			absorptionsKoeff(files, currentDirectory);
		} else {
			normaleAuswertung(files, currentDirectory);
		}
	}

	private void absorptionsKoeff(File[] files, File currentDirectory) {
		try {
			if (files.length != 2) {
				throw new IllegalArgumentException("Keine 2 Dateien ausgew�hlt: " + files.length);
			}
			BufferedReader br1 = new BufferedReader(new FileReader(files[0]));
			String line1 = br1.readLine();
			line1 = br1.readLine();
			BufferedReader br2 = new BufferedReader(new FileReader(files[1]));
			String line2 = br2.readLine();
			line2 = br2.readLine();
			outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, files[1].getName()
					.substring(0, files[1].getName().length() - 4) + "_absorptionskooeffzient.txt")));
			outputWriter.write("lambda\tmu_durch_rho");
			outputWriter.newLine();
			while (line1 != null && line2 != null) {
				int laengeLambda1=line1.length();
				int laengeLambda2=line2.length();
				
				for(int i=0; i<line1.length();i++){
					if(line1.charAt(i)=='\t'){
						laengeLambda1=i;
						break;
					}
				}
				for(int i=0; i<line2.length();i++){
					if(line2.charAt(i)=='\t'){
						laengeLambda2=i;
						break;
					}
				}
				String l1=line1.substring(0, laengeLambda1).trim();
				String l2=line2.substring(0, laengeLambda2).trim();
				double lambda1=Double.valueOf(l1);
				double lambda2=Double.valueOf(l2);
				if(lambda1-lambda2>LAMBDA_TRHESHHOLD){
					System.out.println("Warning: Different Lambdas detected! Delta= "+(lambda1-lambda2));
				}
				
				double I1=Double.valueOf(line1.substring(laengeLambda1+1, line1.length()).trim());
				double I2=Double.valueOf(line2.substring(laengeLambda2+1, line2.length()).trim());
				double mu=-Math.log(I1/I2)/(d*rho);
				outputWriter.write(lambda1+"\t"+mu);
				outputWriter.newLine();
				line1 = br1.readLine();
				line2 = br2.readLine();
			}
			outputWriter.close();
			br1.close();
			br2.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getInformation() {
		return "Passt die R�ntgendaten zum Auswerten an.";
	}

	@Override
	public String getDate() {
		return "07.06.2013";
	}

	public void normaleAuswertung(File files[], File currentDirectory) {
		try {
			for (int i = 0; i < files.length; i++) {
				br = new BufferedReader(new FileReader(files[i]));
				String line = br.readLine();
				if (line != null) {
					outputWriter = new BufferedWriter(new FileWriter(new File(currentDirectory, files[i].getName()
							.substring(0, files[i].getName().length() - 4) + "_conv.txt")));
					outputWriter.write("lambda\tslambda\tI");
					outputWriter.newLine();
					while (line != null) {
						line = line.replaceAll("� :", "\t");
						int zeilenende = line.length();
						for (int j = 0; j < line.length(); j++) {
							if (line.charAt(j) == '/') {
								zeilenende = j;
								break;
							}
						}
						try {
							line = line.substring(0, zeilenende - 1);
						} catch (Exception e) {
							e.printStackTrace();
						}
						String omegaString = line.substring(0, 7);
						double omega = Double.parseDouble(omegaString.trim());
						double lambda = a * Math.sin(omega * 2 * Math.PI / 360) / sqrt2;
						// #############WINKEL IN WELLENL�NGE UMRECHNEN
						// double slambda = Math.sqrt(Math.pow(0.001E-10 *
						// Math.sin(omega * 2 * Math.PI / 360) / sqrt2, 2)
						// + Math.pow(0.02 * 2 * Math.PI / 360 * a *
						// Math.cos(omega * 2 * Math.PI / 360) / sqrt2,
						// 2));
						// line = line.replace(omegaString, "" +
						// lambda+"\t"+slambda);
						// #############WINKEL IN WELLENL�NGE UMRECHNEN ENDE
						// ###############KALIBRIERUNG
						// lambda=0.99398*lambda+0.03515E-10;
						// line = line.replace(omegaString, "" + lambda);
						// ###############KALIBRIERUNG ENDE
						outputWriter.write(line);
						outputWriter.newLine();
						line = br.readLine();
					}
					outputWriter.close();
				}
				br.close();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// public static void main(String args[]){
	// double kante=1.47587785;
	// double [] litkanten=new
	// double[]{0.17892,0.17892,0.17942,0.17960,0.18310,0.18327,0.18438,0.18518,0.20901,0.21383,1.06146,1.06786,1.09857,1.24430,1.26271,1.28181,1.30164,1.42112,1.47631,1.48745};
	// for(int j=0; j<litkanten.length; j++){
	// for(int i=1; i<50; i++){
	// if(Math.abs(i*litkanten[j]-kante)<0.01){
	// System.out.println("Kante: "+litkanten[j]+" Ordnung: "+i+
	// " Wert: "+(i*litkanten[j])+" Differenz: "+(i*litkanten[j]-kante));
	// }
	// }
	// }
	// }
}
